package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.config.RuoYiConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.file.FileUploadUtils;
import com.ruoyi.framework.config.ServerConfig;
import com.ruoyi.system.domain.SysArgumentDocument;
import com.ruoyi.system.domain.SysInformation;
import com.ruoyi.system.service.ISysArgumentDocumentService;
import com.ruoyi.web.controller.common.CommonController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.util.Date;
import java.util.List;


/**
 *论证文件管理
 * @Author zhaoguanyu
 * @Date 13:59 2021/11/29
 */
@Api(tags = "论证文件管理")
@RestController
@RequestMapping("/system/arg")
public class SysArgumentDocumentController extends BaseController {

    private static final Logger log = LoggerFactory.getLogger(CommonController.class);

    @Autowired
    private ServerConfig serverConfig;

    @Autowired
    private ISysArgumentDocumentService sysArgumentDocumentService;
    /*
     * 获取文档列表*/
    @ApiOperation(value = "获取文件列表")
    @PreAuthorize("@ss.hasPermi('document:information:filList')")
    @GetMapping("/{itemNo}")
    public TableDataInfo list(@PathVariable String itemNo){
        List<SysInformation> list = sysArgumentDocumentService.selectDocumentList(itemNo);
        return getDataTable(list);
    }

    /*
    * 上传文档信息*/
    @ApiOperation(value = "论证文件信息添加")
    @PreAuthorize("@ss.hasPermi('document:information:fileAdd')")
    @Log(title = "论证文件管理",businessType = BusinessType.INSERT)
    @PostMapping("/argAdd")
    public AjaxResult add(@RequestBody SysArgumentDocument sysArgumentDocument){
        Date date = new Date();
        sysArgumentDocument.setLastModifiedBy(getUsername());
        sysArgumentDocument.setLastModifiedDate(date);
        return toAjax(sysArgumentDocumentService.updateArgInformation(sysArgumentDocument));
    }

    /**
     *删除论证文件
     */
    @ApiOperation(value = "论证文件删除")
    @PreAuthorize("@ss.hasPermi('document:information:fileremove')")
    @Log(title = "论证文件管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long[] id)
    {
        return toAjax(sysArgumentDocumentService.deleteArgByIds(id));
    }


    /**
     * 上传论证文件
     */
    // todo 上传论证文件之后需要变更项目信息的项目阶段
    @ApiOperation(value = "论证文件上传")
    @PreAuthorize("@ss.hasPermi('document:information:fileupload')")
    @Log(title = "论证文件管理", businessType = BusinessType.IMPORT)
    @PostMapping("/upload")
    public AjaxResult uploadFile(MultipartFile file) throws Exception
    {
        try
        {
            // 上传文件路径
            String filePath = RuoYiConfig.getUploadPath();
            // 上传并返回新文件名称
            String fileName = FileUploadUtils.upload(filePath, file);
            String url = serverConfig.getUrl() + fileName;
            AjaxResult ajax = AjaxResult.success();
            //文件中文名称
            String name = file.getOriginalFilename();
            SysArgumentDocument sysArgumentDocument = new SysArgumentDocument();
            sysArgumentDocument.setFileName(name);
            sysArgumentDocument.setFilePath(filePath);
            sysArgumentDocument.setUrl(url);
            sysArgumentDocument.setOnlyName(fileName);
            Long id = sysArgumentDocumentService.insertFileUpload(sysArgumentDocument);
            ajax.put("id",id);
            ajax.put("fileName", fileName);
            ajax.put("url", url);
            return ajax;
        }
        catch (Exception e)
        {
            return AjaxResult.error(e.getMessage());
        }
    }

    /**
     * 修改论证文件信息
     */
    @ApiOperation(value = "论证文件信息修改")
    @PreAuthorize("@ss.hasPermi('document:information:fileupdate')")
    @Log(title = "论证文件管理", businessType = BusinessType.UPDATE)
    @PutMapping("/argUpdate")
    public AjaxResult updateArg(@Validated @RequestBody SysArgumentDocument sysArgumentDocument) {
        sysArgumentDocument.setUpdateBy(getUsername());
        Date date = new Date();
        sysArgumentDocument.setUpdateTime(date);
        return toAjax(sysArgumentDocumentService.updateInformation(sysArgumentDocument));
    }

}
